/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package config

import (
	"net/rpc"
	"net"
	"gitee.com/tugoer/orivil/log"
	"os"
	"net/http"
	"io/ioutil"
	"path/filepath"
)

type Config struct {
	dir string
}

func (c *Config) Get(name string, reply *[]byte) error {
	data, err := ioutil.ReadFile(filepath.Join(c.dir, name))
	if err != nil {
		return err
	} else {
		*reply = data
		return nil
	}
}

// 运行 rpc 服务器
func RunServer(dir, address string) error {
	cfg := &Config{dir:dir}
	rpc.Register(cfg)
	rpc.HandleHTTP()
	l, e := net.Listen("tcp", address)
	if e != nil {
		return e
	}
	log.Init.Printf("pid:[%d] listen on: [%s]\n", os.Getpid(), address)
	return http.Serve(l, nil)
}
